# Replication code for Figure 3a (and Figure A.1) in Jenke & Sullivan, "Attention and Political Choice: A Foundation for Eye Tracking in Political Science," Political Analysis.
# Written by Libby Jenke

rm(list = ls())

library(ggplot2)
library(dplyr)
devtools::install_github("libbyjenke/PlotEyeFix")
library(PlotEyeFix)

# Insert working directory below
setwd("....")

# Load data ---------------------------------------------------------------
eye_dat <- read.csv("data/sample_data.csv")

# Get data set ---------------------------------------------------------------
# Rename variables and define parameters
fix_data <- eye_dat |>
  rename(
    subjID = Respondent.Name,
    aoi_num = AOI.Label,
    fix_duration = Fixation.Duration,
    fixX = `Fixation.X`,
    fixY = `Fixation.Y`,
    trial_num = trial_num
  )

# Define trial number(s) that you want to examine
trial_number <- 1

# Get rid of any 0s for AOI numbers
fix_data <- fix_data[!is.na(fix_data$aoi_num),]

# Define more parameters for function
image_path <- "data/stimulus_screen.png"
eyetracker_width <- 1920
eyetracker_height <- 1080
trial_num <- 1
output_path <- "output/{subjID}_trial_{trial_num}.png"

# Use the PlotEyeFix function (Jenke and Sullivan, 2025; output will be saved in "output" folder)
PlotEyeFix(fix_data, image_path, eyetracker_width, eyetracker_height, trial_num, output_path)
